﻿using App16.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace App16.Controls
{
    public sealed partial class LoginControl : UserControl
    {
        public LoginControl()
        {
            this.InitializeComponent();
        }
    }

    class LoginControlViewModel : Common.BindableBase
    {
        public LoginControlViewModel()
        {
            App.Adal.AfterLogin += Adal_AfterLogin;
            App.Adal.AfterLogout += Adal_AfterLogout;

            // initial state
            if (App.Adal.UserInfo != null)
                Adal_AfterLogin(this, EventArgs.Empty);
        }

        void Adal_AfterLogin(object sender, EventArgs e)
        {
            if (App.Adal.UserInfo == null)
            {
                this.Name = "Authenticated User";
                this.Email = "Not logged in";
                this.Image = string.Empty;
            }
            else
            {
                this.Name = string.Format("{0} {1}", App.Adal.UserInfo.GivenName, App.Adal.UserInfo.FamilyName);
                this.Email = App.Adal.UserInfo.DisplayableId;
                this.Image = "ms-appx:///Images/Contact.png";
            }
            ProgressVisibility = Visibility.Collapsed;
        }

        void Adal_AfterLogout(object sender, EventArgs e)
        {
            this.Name = "Authenticated User";
            this.Email = "Logged out";
            this.Image = string.Empty;
        }

        string _Image = default(string);
        public string Image { get { return _Image; } set { base.SetProperty(ref _Image, value); } }

        Visibility _ProgressVisibility = Visibility.Collapsed;
        public Visibility ProgressVisibility { get { return _ProgressVisibility; } set { base.SetProperty(ref _ProgressVisibility, value); } }

        string _Name = "Authenticated User";
        public string Name { get { return _Name; } set { base.SetProperty(ref _Name, value); } }

        string _Email = "Awaiting authentication";
        public string Email { get { return _Email; } set { base.SetProperty(ref _Email, value); } }

        RelayCommand _LoginCommand = null;
        public RelayCommand LoginCommand
        {
            get
            {
                if (_LoginCommand != null)
                    return _LoginCommand;
                _LoginCommand = new RelayCommand
                (
                    // this async error (when Windows) is okay
                    // because it is not async when Phone
                    () =>
                    {
                        this.ProgressVisibility = Visibility.Visible;
                        App.Adal.Login(App.ServiceResource);
                    },
                    () => !App.Adal.IsLoggedIn
                );
                this.PropertyChanged += (s, e) => _LoginCommand.RaiseCanExecuteChanged();
                return _LoginCommand;
            }
        }

        RelayCommand _LogoutCommand = null;
        public RelayCommand LogoutCommand
        {
            get
            {
                if (_LogoutCommand != null)
                    return _LogoutCommand;
                _LogoutCommand = new RelayCommand
                (
                    () => App.Adal.Logout(),
                    () => App.Adal.IsLoggedIn
                );
                this.PropertyChanged += (s, e) => _LogoutCommand.RaiseCanExecuteChanged();
                return _LogoutCommand;
            }
        }
    }
}
